package com.jzo2o.foundations.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.IHomeService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
@Service
public class HomeServiceImpl implements IHomeService {

    @Resource
    private RegionMapper regionMapper;

    @Resource
    private ServeMapper serveMapper;

//    public static final String SERVE_BY_ID = "serveByIdCache";

    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,key = "#regionId",cacheManager =RedisConstants.CacheManager.ONE_DAY )
    @Caching(cacheable = {
            //返回数据为空，则缓存空值30分钟，这样可以避免缓存穿透
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,key ="#regionId",unless = "#result.size()>0",cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),

            //返回值不为空，则永久缓存数据
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,key ="#regionId",unless = "#result.size()==0",cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeCategoryResDTO> queryServeIconCategoryByRegionIdCache(Long regionId) {
        // 1、检查当前区域是否启用
        // 根据 regionId 查询区域信息
        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isEmpty(region) || ObjectUtil.notEqual(region.getActiveStatus(), FoundationStatusEnum.ENABLE.getStatus())) {
            // 如果区域不存在或未启用，返回空列表
            return Collections.emptyList();
        }

        // 2、首页服务列表查询, 调用 Dao 执行查询
        // 根据 regionId 查询服务图标分类列表
        List<ServeCategoryResDTO> list = serveMapper.findServeIconCategoryByRegionId(regionId);
        if (CollUtil.isEmpty(list)) {
            // 如果查询结果为空，返回空列表
            return Collections.emptyList();
        }

        // 3、提取前 2 个服务类型和前 4 个服务列表
        // 提取前 2 个服务类型
        list = CollUtil.sub(list, 0, Math.min(list.size(), 2));
        list.forEach(v -> {
            List<ServeSimpleResDTO> serveList = v.getServeResDTOList(); // 获取每个服务类型的服务列表
            serveList = CollUtil.sub(serveList, 0, Math.min(serveList.size(), 4)); // 提取前 4 个服务
            v.setServeResDTOList(serveList); // 更新服务类型的服务列表
        });
// 返回处理后的服务图标分类列表
        return list;
    }

    @Override
    @Caching(
            cacheable = {
                    //返回数据为空，则缓存空值30分钟，这样可以避免缓存穿透
                    @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE, key = "#regionId",
                            unless = "#result.size()>0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),

                    //返回值不为空，则永久缓存数据
                    @Cacheable( value = RedisConstants.CacheName.SERVE_TYPE, key = "#regionId",
                            unless = "#result.size()==0", cacheManager = RedisConstants.CacheManager.FOREVER)
            }
    )
    public List<ServeAggregationTypeSimpleResDTO> queryServeIconCategoryByRegionIdCache2(Long regionId) {
        // 1、检查当前区域是否启用
        // 根据 regionId 查询区域信息
        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isEmpty(region) || ObjectUtil.notEqual(region.getActiveStatus(), FoundationStatusEnum.ENABLE.getStatus())) {
            // 如果区域不存在或未启用，返回空列表
            return Collections.emptyList();
        }

        List<ServeAggregationTypeSimpleResDTO> list = serveMapper.findServeIconCategoryByRegionId2(regionId);
        if (CollUtil.isEmpty(list)) {
            // 如果查询结果为空，返回空列表
            return Collections.emptyList();
        }

        return list;
    }

    /**
     * 首页热门服务列表
     *
     * @param regionId
     * @return
     */
    @Override
    @Caching(cacheable ={
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE, key = "#regionId",
                    unless = "#result.size()>0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE, key = "#regionId",
                    unless = "#result.size()==0", cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeAggregationSimpleResDTO> queryServeIconCategoryByRegionIdCache3(Long regionId) {

        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isEmpty(region) || ObjectUtil.notEqual(region.getActiveStatus(), FoundationStatusEnum.ENABLE.getStatus())) {
            // 如果区域不存在或未启用，返回空列表
            return Collections.emptyList();
        }
        List<ServeAggregationSimpleResDTO> list = serveMapper.findServeIconCategoryByRegionId3(regionId);
        if (CollUtil.isEmpty(list)) {
            // 如果查询结果为空，返回空列表
            return Collections.emptyList();
        }
        return list;
    }

    /**
     *根据id查询服务
     *
     * @param id
     * @return
     */
    @Override
    @Cacheable(value=RedisConstants.CacheName.SERVE_BY_ID, key = "#id")
    public ServeAggregationSimpleResDTO queryServeIconCategoryByRegionIdCache4(Long id) {
        return  serveMapper.findServeIconCategoryByRegionId4(id);
    }
}

